package com.itheima.web.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("test1")
    /*
    * @RequestParam ：对传入的参数进行配置
    * value：如果前端名字和后端名字不一致，使用value进行映射
    * required：设置此参数是否必须
    * defaultValue：为参数设置默认值*/
    public String test1(@RequestParam(value = "username",required = true,defaultValue = "abc") String name, int age){
        System.out.println("username:"+name);
        System.out.println(age==18);
        return "success";
    }

    @RequestMapping("/test2")
    public String test2(User user, String hobbies) {
        System.out.println(user);
        return "success";
    }
    @RequestMapping("/test3")
    public String test3(Date birthday){
        System.out.println(birthday);
        return "success";
    }

    /*
     * 携带数据响应页面的方式（JSP）：
     *   1、原生ServletAPI(不建议使用：尽量解耦)
     *   2、ModelAndView:  值存入request作用域(不建议使用)
     *   3、Model:          值存入request作用域
     *   4、Map:            值存入request作用域
     *
     *   controller跳转到指定controller
     *      redirect:指定请求URL
     *       重定向（客户端跳转）
     *      forward:指定请求URL
     *       请求转发（服务端跳转）
     * */
    @RequestMapping("/test4")
    public String test4(HttpServletRequest request, HttpSession session){
        request.setAttribute("username","admin");
        session.setAttribute("username","123456");
        return "main";
    }
    @RequestMapping("/test5")
    public ModelAndView test5(ModelAndView mv){
        //ModelAndView mv=new ModelAndView();
        mv.setViewName("main");
        mv.addObject("username","xiaodiaochan");
        return mv;
    }

    @RequestMapping("/test6")
    public String test6(Model model){
        //List<User>
        System.out.println("test6....");
        model.addAttribute("username","dadiaochan");
        //return "redirect:/user/update";
        return "forward:/user/update";
    }

    @RequestMapping("/test7")
    public String test7(Map map){
        System.out.println("test7....");
        map.put("username","zhangfei");
        return "main";
    }
}
